package com.geccocrawler.gecco.demo.anjuke;

import com.geccocrawler.gecco.GeccoEngine;
import com.geccocrawler.gecco.annotation.*;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.spider.HtmlBean;
import com.geccocrawler.gecco.utils.ConnectDb;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 抽离 on 2017/5/20 0020.
 */
@Gecco(matchUrl = "http://{corpName}.anjuke.com/gongsi-jjr-{jjrId}/{page}/", pipelines={"consolePipeline", "houseNextPipeline"})
public class AnjukeHouse implements HtmlBean{

    @RequestParameter
    private String corpName;

    @RequestParameter
    private String jjrId;

    @RequestParameter
    private String page;

    @Request
    private HttpRequest httpRequest;

    @Href
    @HtmlField(cssPath = ".houseListTitle")
    private List<String> url;

    @Text
    @HtmlField(cssPath = ".houseListTitle")
    private List<String> title;

    @Attr("src")
    @HtmlField(cssPath = ".item-img>img")
    private List<String> imgUrl;

    @Href
    @HtmlField(cssPath = ".aNxt")
    private String nextUrl;


    public String getCorpName() {
        return corpName;
    }

    public void setCorpName(String corpName) {
        this.corpName = corpName;
    }

    public String getJjrId() {
        return jjrId;
    }

    public void setJjrId(String jjrId) {
        this.jjrId = jjrId;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public HttpRequest getHttpRequest() {
        return httpRequest;
    }

    public void setHttpRequest(HttpRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    public List<String> getUrl() {
        return url;
    }

    public void setUrl(List<String> url) {
        this.url = url;
    }

    public List<String> getTitle() {
        return title;
    }

    public void setTitle(List<String> title) {
        this.title = title;
    }

    public List<String> getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(List<String> imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getNextUrl() {
        return nextUrl;
    }

    public void setNextUrl(String nextUrl) {
        this.nextUrl = nextUrl;
    }

    public static void main(String[] args){
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select url from agent";
        try {
            List<Agent> agents = queryRunner.query(connection, sql, new BeanListHandler<Agent>(Agent.class));
            List<HttpRequest> requests = new ArrayList<>();
            for(Agent agent : agents){
                HttpGetRequest httpGetRequest = new HttpGetRequest(agent.getUrl());
                requests.add(httpGetRequest);
            }

          GeccoEngine.create().classpath("com.geccocrawler.gecco.demo.anjuke").thread(150).retry(10).start("http://maitianfangchan5.anjuke.com/gongsi-jjr-2382106/p1/").run();
//            GeccoEngine.create().classpath("com.geccocrawler.gecco.demo.anjuke").thread(150).retry(10).start(requests).run();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void test(){

    }
}
